const Koa = require('koa');
const app = new Koa();
const axios = require('axios')
const JWT = require('jsonwebtoken')
const route = require('koa-route')
const bodyParser = require('koa-bodyparser');

app.use(bodyParser());
app.use(route.post('/api/login', async ctx => {
	const { code } = ctx.request.body;
	// 请求微信官网服务器 携带 code
	const resp = await axios.get('https://api.weixin.qq.com/sns/jscode2session', {
		params: {
			js_code: code,
			appid: 'wx2bae7e5b372f752d',
			secret: 'ee5666c19763d96f3023c0b45770c500',
			grant_type: 'authorization_code'
		}
	})
	// 将保存到开发者服务器， 同时生成token
	const { openid, session_key } = resp.data;
	const token = JWT.sign({ openid, session_key }, '666666')

	ctx.body = {
		code: 200,
		data: {
			token,

				
		},
		message: '登录成功！！！'
	}
}))

app.listen(3000);